package com.qianfeng.openapi.web.master.controller;

import com.github.pagehelper.PageInfo;
import com.qianfeng.openapi.web.master.bean.AjaxResponse;
import com.qianfeng.openapi.web.master.bean.TableData;
import com.qianfeng.openapi.web.master.pojo.Menu;
import com.qianfeng.openapi.web.master.pojo.Role;
import com.qianfeng.openapi.web.master.service.MenuService;
import com.qianfeng.openapi.web.master.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author LJF
 * @Date 2020/7/4 9:02
 * @Version 1.0
 */
@RestController
@RequestMapping("/sys/role")
@CrossOrigin(allowCredentials = "true")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private MenuService menuService;

    @RequestMapping("/table")
    public TableData<Role> table(@RequestParam(defaultValue = "1") Integer page,
                                 @RequestParam(defaultValue = "5") Integer limit){
        PageInfo<Role> pageInfo = roleService.getAllRoles(page, limit);
        TableData<Role> roleTableData = new TableData<>();
        roleTableData.setCount(pageInfo.getTotal());
        roleTableData.setData(pageInfo.getList());
        return roleTableData;
    }

    @RequestMapping("/add")
    public AjaxResponse add(Role role){
        try{
            roleService.insertRole(role);
            return new AjaxResponse(true,"添加成功!");
        }catch (Exception ex){
            ex.printStackTrace();
            return new AjaxResponse(false,"添加失败!");
        }

    }

    @RequestMapping("/update")
    public AjaxResponse update(Role role){
        try{
            roleService.updateRole(role);
            return new AjaxResponse(true,"更新成功!");
        }catch (Exception ex){
            ex.printStackTrace();
            return new AjaxResponse(false,"更新失败!");
        }
    }

    @RequestMapping("/search")
    public TableData<Role> search(@RequestParam(defaultValue = "1") Integer page,
                                  @RequestParam(defaultValue = "5") Integer limit
                                  ,String name,Integer status){
        PageInfo<Role> pageInfo = roleService.getRoleByCondition(page, limit,name,status);
        TableData<Role> roleTableData = new TableData<>();
        roleTableData.setCount(pageInfo.getTotal());
        roleTableData.setData(pageInfo.getList());
        return roleTableData;

    }

    @RequestMapping("/role_menu")
    public List<Integer> roleMenu(Integer roleId){
        return roleService.getMenuIdsByRoleId(roleId);
    }

    @RequestMapping("/menu_tree")
    public List<Menu> menuTree(){
        return menuService.getFullMenu();
    }

    @RequestMapping("/assign_menu")
    public AjaxResponse assignMenu(Integer roleId,Integer[] menuIds){
        try {
            roleService.updateRoleMenu(roleId,menuIds);
            return new AjaxResponse(true,"授权成功");
        }catch (Exception exception){
            exception.printStackTrace();
            return new AjaxResponse(false,"授权失败");
        }
    }

}
